package com.jlect.swebing.ui.laf.basic.client;

import com.google.gwt.user.client.Element;
import com.jlect.swebing.ui.client.GComponent;
import com.jlect.swebing.ui.client.laf.ComponentUI;

import java.util.HashMap;
import java.util.Map;

/**
 * Store and register information about what element is bound to what component ui
 *
 * @author Sergey Kozmin
 * @since 25.11.2007 15:47:32
 */
public class ComponentsRegister {
    private static ComponentsRegister INSTANCE = new ComponentsRegister();

    private Map components = new HashMap();

    private ComponentsRegister() {
    }

    /**
     * Retrievs component that is bound to the given
     * @param element element
     * @return bound component
     */
    public GComponent getComponent(Element element) {
        return (GComponent) components.get(element);
    }

    /**
     * Retisters the given component and its element.
     * @param element component's element
     * @param component component to register
     */
    public void registerComponent(Element element, ComponentUI component) {
        components.put(element, component);
    }

    /**
     * Cancels the registration of the component
     * @param element element to be unregistered
     * @return registered component
     */
    public GComponent cancelRegistration(Element element) {
        return (GComponent) components.remove(element);
    }

    public static ComponentsRegister getRegister() {
        return INSTANCE;
    }
}
